package com.shhm.common.utils;

import com.alibaba.fastjson2.JSON;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.shhm.common.constants.HttpStatus;
import com.shhm.common.dto.response.AjaxResultPlus;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;

/**
 * @author JackZ
 * @version 1.0
 * @description: 处理响应工具类
 * @date 2025/8/2 下午12:34
 */
public class ResponseUtil {
    private static final ObjectMapper objectMapper = new ObjectMapper();

    public static AjaxResultPlus sqlExeIsSuccess(int effRow) {
        if (effRow<=0) return AjaxResultPlus.error("操作失败，请刷新页面或稍后重试！");
        else return AjaxResultPlus.success(true);
    }

    public static AjaxResultPlus sqlExeIsSuccess(boolean effRow) {
        if (!effRow) return AjaxResultPlus.error("操作失败，请刷新页面或稍后重试！");
        else return AjaxResultPlus.success(true);
    }

    public static AjaxResultPlus sqlExeIsSuccess(int effRow,String message) {
        if (effRow<=0) return AjaxResultPlus.error(message);
        else return AjaxResultPlus.success(true);
    }


    public static void sendErrorResponse(HttpServletResponse response, int code, String message) throws IOException {
        response.setContentType("application/json;charset=utf-8");
        response.setStatus(code);
        AjaxResultPlus errorResult = AjaxResultPlus.error(code,message);
        response.getWriter().write(JSON.toJSONString(errorResult));
        response.getWriter().flush();
    }
}
